package top.korori.common.utils;

import cn.hutool.core.util.IdUtil;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import top.korori.common.constant.FileTypeConstant;
import top.korori.common.exception.BizException;

import java.util.Arrays;
import java.util.Base64;

public class ImageUtils {

     public static MultipartFile base64ToMultipartFile(String base64){
         String[] parts = base64.split(",");
         String contentType = parts[0].split(";")[0].split(":")[1];
         boolean isImg = Arrays.asList(FileTypeConstant.IMAGE_CONTENT_TYPE).contains(contentType);
         if (!isImg){
             BizException.cast("不支持该类型文件");
         }
         byte[] bytes = Base64.getDecoder().decode(parts[1]);
         return new MockMultipartFile("file", IdUtil.fastSimpleUUID()+"."+contentType.split("/")[1], contentType, bytes);
     }

}
